<template>
  <div>
    <tableComponents
      :pagination="{ currentPage, pageSize, total: list.all_num }"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="editrider"
      addText="添加骑手"
    >
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="loading" header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%">
          <el-table-column label="头像" min-width="100" align="center">
            <template slot-scope="scope">
              <div class="tc">
                <img style="width: 50px; height: 50px" :src="$fnc.getImgUrl(scope.row.avatar)" class="user_img" />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="骑手信息" min-width="660">
            <template slot-scope="scope">
              <div class="user_table">
                <span>用户昵称：</span>
                <small>{{ scope.row.nickname || "----" }}</small>
                <br />
                <span>会员账号：</span>
                <small>{{ scope.row.username }}</small>
                <br />
                <span>注册时间：</span>
                <small>{{ scope.row.created_time | getTimeFormat }}</small>
                <br />
                <span>配送区域：</span>
                <small>{{ $fnc.deleteNumber(`${scope.row.rider_province || ""}${scope.row.rider_city || ""}${scope.row.rider_area || ""}${scope.row.rider_town || ""}`) }}</small>
                <br />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="上下班状态" min-width="65">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_rider_work"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="on"
                inactive-text="off"
                active-value="1"
                @change="handleSwitch($event, scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="80">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="editrider(scope.row)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deleteNew(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" title="配送" :modal-append-to-body="false" :visible.sync="showrider" class="tl size_13" width="800px">
      <el-form :model="rider" ref="data" label-width="120px" v-if="showrider">
        <el-form-item label="用户名" prop="uid">
          <el-col :span="12">
            <el-input placeholder="请选择用户" v-model="rider.nickname" :disabled="true" clearable></el-input>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-button v-if="isRider" :loading="$store.state.isLoading" size="medium" type="primary" @click="xzshop('xzuser')">选择用户</el-button>
          </el-col>
        </el-form-item>
        <el-form-item label="配送范围" prop>
          <el-col :span="12">
            <city-cascader :placeholder="selectedOptions.join('-')" :level="2" @change="handleChange" />
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="subrider">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <keep-alive>
      <component :is="current" :show="shows" :isAll="false" showType="list" @sendShop="getSend" @isshow="getShow"></component>
    </keep-alive>
  </div>
</template>
<script>
import cityCascader from "@/components/currency/cityCascader/cityCascader";
import xzuser from "@/components/currency/xz_user.vue";
import { riderListSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: riderListSearchData,
      current: "",
      shows: false,
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      arr: [],
      searchAll: {},
      row: {},
      selectedOptions: [],
      showrider: false,
      options: this.$addressJson,
      optionProps: {
        value: "y",
        label: "t",
        children: "z",
        checkStrictly: true,
      },
      rider: {
        is_rider: "0",
        rider_province: "",
        rider_city: "",
        rider_area: "",
      },
      showCity: true,
      isRider: true,
    };
  },
  components: {
    cityCascader,
    xzuser,
  },
  methods: {
    handleSwitch(val, item) {
      var params = item;
      params.is_rider_work = val;
      this.$api.getRider.saveRider(params).then((res) => {
        if (res.data.code != 200) {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getNewsAll();
        }
      });
    },
    xzshop(str) {
      this.current = str;
      this.shows = true;
    },
    getShow(val) {
      this.shows = val;
    },
    getSend(row) {
      if (this.current == "xzuser") {
        this.rider.nickname = row.nickname + "(" + row.username + ")";
        this.rider.id = row.id;
      }
    },
    changeRadio() {
      this.showCity = false;
      setTimeout(() => {
        this.showCity = true;
      }, 500);
    },
    editrider(item) {
      if (item) {
        this.isRider = false;
        this.rider.id = item.id;
        this.rider.nickname = item.nickname + "(" + item.username + ")";
        this.$nextTick(() => {
          this.selectedOptions = [item.rider_province, item.rider_city, item.rider_area];

          if (item.rider_town) {
            this.selectedOptions.push(item.rider_town);
          }
        });
      } else {
        this.rider = {};
        this.isRider = true;
      }
      this.showrider = true;
    },
    subrider() {
      this.showrider = false;
      var params = this.rider || {};

      this.$api.getRider.addRider(params).then((res) => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("操作成功");
          this.getNewsAll(this.searchAll);
        }
      });
    },
    handleChange(value) {
      this.rider.rider_province = "";
      this.rider.rider_city = "";
      this.rider.rider_area = "";
      this.rider.rider_town = "";
      if (value) {
        for (var i = 0; i < value.length; i++) {
          if (i == 0) this.rider.rider_province = value[i];
          if (i == 1) this.rider.rider_city = value[i];
          if (i == 2) this.rider.rider_area = value[i];
          if (i == 3) this.rider.rider_town = value[i];
        }
      }
      console.log(this.rider);
    },
    getOpenThis() {
      this.show_supplier = false;
    },
    getUserInfo(id) {
      this.$router.push({
        path: "/index/user/user_info",
        query: { id: id },
      });
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;

      this.$api.getRider.getRiderList(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    deleteNew(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getRider.delRider(data).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");

              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
            }
          });
        }
      });
    },
    reload() {
      this.getNewsAll(this.searchAll);
    },
  },
  beforeCreate() {},
  created() {
    this.getNewsAll();

    this.$api.getUser.getExtractList({ page_size: 999 }).then((res) => {
      if (res.data.code == 200) {
        this.$store.commit("setExtractList", res.data.result.data);
      }
    });
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
